package controller.v2;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import model.AddressVO;
import model.CityVO;
import model.MemberVO;
import service.v2.AddressService;
import service.v2.CityService;
import service.v2.MemberService;
import util.PackageUtil;

@Controller
@RequestMapping("/v2/address")
public class AddressController {
	@Autowired
	private AddressService addressService;
	@Autowired
	private MemberService memberService;
	@Autowired
	private CityService cityService;
	/**
	 * @param token 令牌
	 * @param member_id 用户ID
	 * @return 地址列表
	 */
	@ResponseBody
	@RequestMapping(value="/addressList",method=RequestMethod.GET) 
	public Object addressList(@RequestHeader (value="LANG", required=false) String lang,@RequestHeader (value="AUTHORIZATION", required=false) String token,@RequestHeader (value="SECRET", required=false)  String member_id,HttpServletRequest request, HttpServletResponse response, MemberVO memberVO){
		List<AddressVO> addressList = addressService.getAddressListByMemberId(member_id);
		return PackageUtil.packageParams(200,"successs",addressList,""); 
	}
	/**
	 * @param token 令牌
	 * @param member_id 用户ID
	 * @param name 姓名
	 * @param tel 手机号
	 * @param city 三级城市名
	 * @param city_id 三级城市ID
	 * @param address 详细地址
	 * @param default_yn 是否为默认 Y或者N
	 * @return 新建地址提交
	 */
	@ResponseBody
	@RequestMapping(value="/addAddress",method=RequestMethod.POST) 
	public Object addAddress(@RequestHeader (value="LANG", required=false) String lang,@RequestHeader (value="AUTHORIZATION", required=false) String token,@RequestHeader (value="SECRET", required=false)  String member_id,HttpServletRequest request, HttpServletResponse response
			, MemberVO memberVO,AddressVO addressVO){
		//姓名
		if(addressVO.getName()==null || "".equals(addressVO.getName())){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		if(addressVO.getName().length() > 80){
			return PackageUtil.packageParams(10000,"参数错误，请参考API文档","",""); 
		}
		//电话
		if(addressVO.getTel()==null || "".equals(addressVO.getTel())){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		if(addressVO.getTel().length() > 30){
			return PackageUtil.packageParams(10000,"参数错误，请参考API文档","",""); 
		}
		//城市
		if(addressVO.getCity()==null || "".equals(addressVO.getCity())){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		if(addressVO.getCity().length() > 200){
			return PackageUtil.packageParams(10000,"参数错误，请参考API文档","",""); 
		}
		//地址
		if(addressVO.getAddress()==null || "".equals(addressVO.getAddress())){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		if(addressVO.getAddress().length() > 300){
			return PackageUtil.packageParams(10000,"参数错误，请参考API文档","",""); 
		}
		//城市ID
		if(addressVO.getCity_id()==null || "".equals(addressVO.getCity_id())){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		//验证城市ID是否错误
		CityVO cityVO = cityService.checkCityLevelAndId(addressVO.getCity_id());
		if(cityVO == null){
			return PackageUtil.packageParams(10000,"参数错误，请参考API文档","",""); 
		}
		//是否为默认
		if(addressVO.getDefault_yn()==null || "".equals(addressVO.getDefault_yn())){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		if(!"Y".equals(addressVO.getDefault_yn()) &&!"N".equals(addressVO.getDefault_yn())  ){
			return PackageUtil.packageParams(10000,"参数错误，请参考API文档","",""); 
		}
		addressVO.setMember_id(member_id);
		//检查是否有地址 没有设置为默认  改成可以没有默认了
//		int num = addressService.getAddressNum(member_id);
//		if(num==0){
//			addressVO.setDefault_yn("Y");
//		}
		addressService.insertAddresss(addressVO);
		return PackageUtil.packageParams(200,"successs",addressVO,""); 
	}
	/**
	 * @param token 令牌
	 * @param  用户ID
	 *  @param id 地址ID
	 * @return 地址列表
	 */
	@ResponseBody
	@RequestMapping(value="/getAddress/{id}",method=RequestMethod.GET) 
	public Object getAddress(@PathVariable String id,@RequestHeader (value="AUTHORIZATION", required=false) String token,@RequestHeader (value="SECRET", required=false)  String member_id,HttpServletRequest request, HttpServletResponse response, MemberVO memberVO){
		AddressVO addressVO = addressService.getAddressById(id);
		return PackageUtil.packageParams(200,"successs",addressVO,""); 
	}
	/**
	 * @param token 令牌
	 * @param member_id 用户ID
	 * @param name 姓名
	 * @param tel 手机号
	 * @param city 城市字符串
	 * @param address 详细地址
	 * @param default_yn 是否为默认 Y或者N
	 * @param  地址ID
	 * @return 修改地址提交
	 */
	@ResponseBody
	@RequestMapping(value="/updateAddress",method=RequestMethod.POST) 
	public Object updateAddress(@RequestHeader (value="LANG", required=false) String lang,@RequestHeader (value="AUTHORIZATION", required=false) String token,@RequestHeader (value="SECRET", required=false)  String member_id,HttpServletRequest request, HttpServletResponse response
			, MemberVO memberVO,AddressVO addressVO){
		//地址ID
		if(addressVO.getAddress_id()==null || "".equals(addressVO.getAddress_id())){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		//姓名
		if(addressVO.getName()==null || "".equals(addressVO.getName())){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		if(addressVO.getName().length() > 80){
			return PackageUtil.packageParams(10000,"参数错误，请参考API文档","",""); 
		}
		//电话
		if(addressVO.getTel()==null || "".equals(addressVO.getTel())){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		if(addressVO.getTel().length() > 30){
			return PackageUtil.packageParams(10000,"参数错误，请参考API文档","",""); 
		}
		//城市
		if(addressVO.getCity()==null || "".equals(addressVO.getCity())){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		if(addressVO.getCity().length() > 200){
			return PackageUtil.packageParams(10000,"参数错误，请参考API文档","",""); 
		}
		//地址
		if(addressVO.getAddress()==null || "".equals(addressVO.getAddress())){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		if(addressVO.getAddress().length() > 300){
			return PackageUtil.packageParams(10000,"参数错误，请参考API文档","",""); 
		}
		//城市ID
		if(addressVO.getCity_id()==null || "".equals(addressVO.getCity_id())){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		//验证城市ID是否错误
		CityVO cityVO = cityService.checkCityLevelAndId(addressVO.getCity_id());
		if(cityVO == null){
			return PackageUtil.packageParams(10000,"参数错误，请参考API文档","",""); 
		}
		//是否为默认
		if(addressVO.getDefault_yn()==null || "".equals(addressVO.getDefault_yn())){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		if(!"Y".equals(addressVO.getDefault_yn()) &&!"N".equals(addressVO.getDefault_yn())  ){
			return PackageUtil.packageParams(10000,"参数错误，请参考API文档","",""); 
		}
		addressVO.setMember_id(member_id);
		addressService.updateAddresss(addressVO);
		return PackageUtil.packageParams(200,"successs","",""); 
	}
	/**
	 * @param token 令牌
	 * @param member_id 用户ID
	 * @param  地址ID
	 * @return 删除地址
	 */
	@ResponseBody
	@RequestMapping(value="/deleteAddress",method=RequestMethod.POST) 
	public Object deleteAddress(@RequestHeader (value="AUTHORIZATION", required=false) String token,@RequestHeader (value="SECRET", required=false)  String member_id,HttpServletRequest request, HttpServletResponse response
			, MemberVO memberVO,AddressVO addressVO){
		//地址ID
		if(addressVO.getAddress_id()==null || "".equals(addressVO.getAddress_id())){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		addressVO.setMember_id(member_id);
		addressService.deleteAddresss(addressVO);
		return PackageUtil.packageParams(200,"successs","",""); 
	}
	
	
	/**
	 * 国际版地址列表
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/addressListEn",method=RequestMethod.GET) 
	public Object addressListEn(@RequestHeader (value="LANG", required=false) String lang,@RequestHeader (value="AUTHORIZATION", required=false) String token,@RequestHeader (value="SECRET", required=false)  String member_id,HttpServletRequest request, HttpServletResponse response, MemberVO memberVO){
		List<AddressVO> addressList = addressService.getAddressListEnByMemberId(member_id);
		return PackageUtil.packageParams(200,"successs",addressList,""); 
	}
	@ResponseBody
	@RequestMapping(value="/addAddressEn",method=RequestMethod.POST) 
	public Object addAddressEn(@RequestHeader (value="LANG", required=false) String lang,@RequestHeader (value="AUTHORIZATION", required=false) String token,@RequestHeader (value="SECRET", required=false)  String member_id,HttpServletRequest request, HttpServletResponse response
			, MemberVO memberVO,AddressVO addressVO){
		if(addressVO.getName()==null || "".equals(addressVO.getName())){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		if(addressVO.getName().length() > 80){
			return PackageUtil.packageParams(10000,"参数错误，请参考API文档","",""); 
		}
		if(addressVO.getCountry_id()==null || "".equals(addressVO.getCountry_id())){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		if(addressVO.getTel()==null || "".equals(addressVO.getTel())){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		if(addressVO.getAddress()==null || "".equals(addressVO.getAddress())){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		//是否为默认
		if(addressVO.getDefault_yn()==null || "".equals(addressVO.getDefault_yn())){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		if(!"Y".equals(addressVO.getDefault_yn()) &&!"N".equals(addressVO.getDefault_yn())  ){
			return PackageUtil.packageParams(10000,"参数错误，请参考API文档","",""); 
		}
		if(addressVO.getCountry()==null || "".equals(addressVO.getCountry())){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		if(addressVO.getProvince()==null || "".equals(addressVO.getProvince())){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		if(addressVO.getProvince().length() > 80){
			return PackageUtil.packageParams(10000,"参数错误，请参考API文档","",""); 
		}
		if(addressVO.getCity()==null || "".equals(addressVO.getCity())){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		if(addressVO.getCity().length() > 80){
			return PackageUtil.packageParams(10000,"参数错误，请参考API文档","",""); 
		}
		if(addressVO.getCode()==null || "".equals(addressVO.getCode())){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		if(addressVO.getCode().length() != 5){
			return PackageUtil.packageParams(10000,"参数错误，请参考API文档","",""); 
		}
		addressVO.setMember_id(member_id);
		addressService.insertAddresssEn(addressVO);
		return PackageUtil.packageParams(200,"successs",addressVO,""); 
	}
	@ResponseBody
	@RequestMapping(value="/getAddressEn/{id}",method=RequestMethod.GET) 
	public Object getAddressEn(@PathVariable String id,@RequestHeader (value="AUTHORIZATION", required=false) String token,@RequestHeader (value="SECRET", required=false)  String member_id,HttpServletRequest request, HttpServletResponse response, MemberVO memberVO){
		AddressVO addressVO = addressService.getAddressByIdEn(id);
		return PackageUtil.packageParams(200,"successs",addressVO,""); 
	}
	@ResponseBody
	@RequestMapping(value="/deleteAddressEn",method=RequestMethod.POST) 
	public Object deleteAddressEn(@RequestHeader (value="AUTHORIZATION", required=false) String token,@RequestHeader (value="SECRET", required=false)  String member_id,HttpServletRequest request, HttpServletResponse response
			, MemberVO memberVO,AddressVO addressVO){
		//地址ID
		if(addressVO.getAddress_id()==null || "".equals(addressVO.getAddress_id())){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		addressVO.setMember_id(member_id);
		addressService.deleteAddresssEn(addressVO);
		return PackageUtil.packageParams(200,"successs","",""); 
	}
	@ResponseBody
	@RequestMapping(value="/updateAddressEn",method=RequestMethod.POST) 
	public Object updateAddressEn(@RequestHeader (value="LANG", required=false) String lang,@RequestHeader (value="AUTHORIZATION", required=false) String token,@RequestHeader (value="SECRET", required=false)  String member_id,HttpServletRequest request, HttpServletResponse response
			, MemberVO memberVO,AddressVO addressVO){
		if(addressVO.getName()==null || "".equals(addressVO.getName())){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		if(addressVO.getName().length() > 80){
			return PackageUtil.packageParams(10000,"参数错误，请参考API文档","",""); 
		}
		if(addressVO.getCountry_id()==null || "".equals(addressVO.getCountry_id())){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		if(addressVO.getTel()==null || "".equals(addressVO.getTel())){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		if(addressVO.getAddress()==null || "".equals(addressVO.getAddress())){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		//是否为默认
		if(addressVO.getDefault_yn()==null || "".equals(addressVO.getDefault_yn())){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		if(!"Y".equals(addressVO.getDefault_yn()) &&!"N".equals(addressVO.getDefault_yn())  ){
			return PackageUtil.packageParams(10000,"参数错误，请参考API文档","",""); 
		}
		if(addressVO.getCountry()==null || "".equals(addressVO.getCountry())){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		if(addressVO.getProvince()==null || "".equals(addressVO.getProvince())){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		if(addressVO.getProvince().length() > 80){
			return PackageUtil.packageParams(10000,"参数错误，请参考API文档","",""); 
		}
		if(addressVO.getCity()==null || "".equals(addressVO.getCity())){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		if(addressVO.getCity().length() > 80){
			return PackageUtil.packageParams(10000,"参数错误，请参考API文档","",""); 
		}
		if(addressVO.getCode()==null || "".equals(addressVO.getCode())){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		if(addressVO.getCode().length() != 5){
			return PackageUtil.packageParams(10000,"参数错误，请参考API文档","",""); 
		}
		addressVO.setMember_id(member_id);
		addressService.updateAddresssEn(addressVO);
		return PackageUtil.packageParams(200,"successs","",""); 
	}
}
